package controller;

import dao.PersonDao;
import dao.impl.PersonDaoImpl;
import entity.Person;
import service.PersonService;
import service.impl.PersonServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;


/**
 * 方法名称开头必须小写
 *  名字必须合规,名字通常用英文缩写，要言简意赅
 */
@WebServlet(urlPatterns = "/personController")
public class PersonController extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String op = req.getParameter("op");


        if ("del".equals(op)){
            int id= Integer.parseInt(req.getParameter("id"));
            PersonDao dao=new PersonDaoImpl();
            PrintWriter out=resp.getWriter();
            boolean a=false;
            try {
                a=dao.delPerson(id);
            } catch (SQLException | ClassNotFoundException e) {
                e.printStackTrace();
            }
            if (a){
                out.write("删除成功");
            }else{
                out.write("删除失败");
            }
        }else if ("list".equals(op)){
            PersonService p=new PersonServiceImpl();
            List<Person> list;
            try {
                list = p.getPersonList();
                req.setAttribute("list",list);
                req.getRequestDispatcher("list.jsp").forward(req,resp);
            } catch (SQLException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }else if ("add".equals(op)){
            String name= req.getParameter("name");
            String sex = req.getParameter("sex");
            String age = req.getParameter("age");
            String rank = req.getParameter("rank");
            String department = req.getParameter("department");
            PersonService p=new PersonServiceImpl();
            try {
                p.addPerson(name,sex,Integer.parseInt(age),rank,department);
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        } else if ("update".equals(op)){
            PrintWriter out=resp.getWriter();
            int id= Integer.parseInt(req.getParameter("id"));
            String name= req.getParameter("name");
            String sex= req.getParameter("sex");
            int age= Integer.parseInt(req.getParameter("age"));
            String rank= req.getParameter("rank");
            String department= req.getParameter("department");
            PersonService p = new PersonServiceImpl();
            boolean b = false;
            try {
                b =p.updatePerson(name,sex,age,rank,department,id);
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            if (b){
                out.write("修改成功");
            }else{
                out.write("修改失败");
            }
        }
    }


    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

}
